<?php
require_once(APPPATH.'views/components/page.php');
require_once(APPPATH.'libraries/newsobject.php');
require_once (APPPATH.'libraries/paginationobject.php');

/**
 * Branch controller
 */
class News extends CI_Controller
{
	
	function __construct()
	{
		parent::__construct();
		
		$this->load->model('news_model');
	}
	
	public function index($promotion = 0, $curPage = 0)
	{
		$rec_limit = NEWS_REC_LIMIT;
			
		if ($curPage == 0)
		{
			$offset = 0;
			$page = 0;
		}
		else {
			$page = $curPage - 1;
			$offset = $rec_limit * $page;
		}
		
		$data['newsarr'] = $this->news_model->getAllNews($offset, $rec_limit, $promotion);
		$total = $this->news_model->countNews($promotion);
		$config = PaginationObject::getInstance()->createPaginationConfig('news/index/'.$promotion.'/', $total, $curPage, 4);
		$this->pagination->initialize($config);
		$data['paging_link'] = $this->pagination->create_links();
		$data['rpromotion'] = $promotion;
		
		if (Page::getInstance()->isAdmin())
		{
			$this->load->view("news/news_admin_view", $data);
		}
		else
		{
			$this->load->view("news_view", $data);
		}
	}
	
	public function insert()
	{
		if (Page::getInstance()->isAdmin())
		{
			$this->load->view('news/insert_view');
		}
	}
	
	public function processInsert()
	{
		if (Page::getInstance()->isAdmin())
		{
			$error = ERROR_NONE;
			$id = $this->input->post('newsid');
			$title = $this->input->post('title');
			$content = $this->input->post('newscontent');
			$image = $this->input->post('image');
			$promotion = $this->input->post('promotion') != NULL ? TRUE : FALSE;
			
			log_message('error', 'pro='.$promotion);
			
			$news = new NewsObject($id, $title, $content, $image, $promotion);
			
			if ($id < 0)
				$error = $this->news_model->insertNews($news);
			else
				$error = $this->news_model->updateNews($id, $news);
			
			$this->session->set_userdata('error', $error);
			redirect('news');
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function delete()
	{
		if (Page::getInstance()->isAdmin())
		{
			$error = ERROR_NONE;
			$ids = $this->input->post('newsids');
			
			$error = $this->news_model->deleteMultipleNews($ids);
			
			$this->session->set_userdata('error', $error);
			redirect('news');
		}
		else
		{
			redirect(site_url());
		}
	}
	
	public function view($id)
	{
		$news = $this->news_model->getNewsById($id);
		
		if ($news->getId() <= 0)
			$data['rerror'] = NOT_EXISTED_ERROR;
		
		$data['news'] = $news;
		
		if (Page::getInstance()->isAdmin())
		{
			$this->load->view('news/insert_view', $data);
		}
		else
		{
			$data['related_news'] = $this->news_model->getRelatedNews(RELATED_ITEMS, $news->isPromotion(), $news->getId());
			$this->load->view('news/news_detail_view', $data);
		}
	}

	public function viewByType($promotion)
	{
		redirect('news/'.$promotion.'/0');
	}
	
	public function update()
	{
		if (Page::getInstance()->isAdmin())
		{
			$error = ERROR_NONE;
			$title = $this->input->post('title');
			$id = $this->input->post('newsid');
			$content = $this->input->post('content');
			$image = $this->input->post('image');
			$promotion = $this->input->post('promotion') != NULL ? TRUE : FALSE;
			
			$news = $this->news_model->getNewsById($id);
			$news->setTitle($title);
			$news->setContent($content);
			$news->setImage($image);
			$news->setPromotion($promotion);
			
			$error = $this->news_model->updateNews($id, $news);
			
			$this->session->set_userdata('error', $error);
			
			if ($error == ERROR_NONE)
			{
				redirect('news');
			}
			else {
				redirect('news/view/'.$id);
			}
		}
		else
		{
			redirect(site_url());
		}
	}
}
/* End of file news.php */
/* Location: ./application/controllers/news.php */